<?php


namespace app\erp\controller;


use app\erp\model\MonthContractdb;

class MonthContract extends Base
{
    protected $cols = array(
        array(
            'field' => 'id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'month',
            'width' => 160,
            'title' => '日期'
        ),
        array(
            'field' => 'supplier_name',
            'width' => 240,
            'title' => '供应商'
        ),
        array(
            'field' => 'contract_file',
            'width' => 160,
            'title' => '合同文件',
            'templet' => '#contract_fileTpl'
        ),
        array(
            'field' => 'upload_file',
            'width' => 160,
            'title' => '回传文件',
            'templet' => '#upload_fileTpl'
        ),
        array(
            'field' => 'audit',
            'width' => 100,
            'title' => '审核状态',
            'templet' => '#auditTpl'
        ),
        array(
            'field' => 'audit_name',
            'width' => 120,
            'title' => '审核人'
        ),
        array(
            'field' => 'audit_time',
            'width' => 160,
            'title' => '审核时间'
        ),
        array(
            'field' => 'reason',
            'width' => 160,
            'title' => '驳回原因'
        ),
        array(
            'fixed' => 'right',
            'width' => 80,
            'title' => '操作',
            'toolbar' => '#month_contract_tool'
        )
    );
    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
    }

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function ajaxList()
    {
        $monthdb = new MonthContractdb();
        $monthdb->param = $this->post;
        $result = $monthdb
            ->order('id desc')
            ->paginate($this->post['limit']);
        $this->layuiReturn($result->items(), $result->total());
    }

    /**
     * Title("审核通过")
     */
    public function pass()
    {
        $monthdb = new MonthContractdb();
        $get_contract = $monthdb->find($this->post['id']);
        if ($get_contract) {
            $save_data = [];
            $save_data['audit_id'] = MEMBER_ID;
            $save_data['audit_time'] = date('Y-m-d H:i:s');
            $save_data['audit'] = 1;
            $get_contract->save($save_data);
        }
        $this->layuiReturn('审核成功');
    }

    /**
     * Title("批量通过")
     */
    public function ajaxAllPass()
    {
        $monthdb = new MonthContractdb();
        $get_contract = $monthdb->whereIn('id', $this->post['id_arr'])->select();
        if (!$get_contract->isEmpty()) {
            $save_data = [];
            foreach ($get_contract as $key => $val) {
                $temp = [];
                $temp['audit_id'] = MEMBER_ID;
                $temp['audit_time'] = date('Y-m-d H:i:s');
                $temp['audit'] = 1;
                $temp['id'] = $val['id'];
                array_push($save_data, $temp);
            }
            $monthdb->saveAll($save_data);
        }
        $this->layuiReturn('success', 1, '操作成功', 1);
    }

    /**
     * Title("批量驳回")
     */
    public function ajaxAllRefuse()
    {
        $monthdb = new MonthContractdb();
        $get_contract = $monthdb->whereIn('id', $this->post['id_arr'])->select();
        if (!$get_contract->isEmpty()) {
            $save_data = [];
            foreach ($get_contract as $key => $val) {
                $temp = [];
                $temp['audit_id'] = MEMBER_ID;
                $temp['audit_time'] = date('Y-m-d H:i:s');
                $temp['audit'] = 2;
                $temp['id'] = $val['id'];
                $temp['reason'] = $this->post['remark'];
                array_push($save_data, $temp);
            }
            $monthdb->saveAll($save_data);
        }
        $this->layuiReturn('success', 1, '操作成功', 1);
    }

    /**
     * Title("驳回申请")
     */
    public function reject()
    {
        $monthdb = new MonthContractdb();
        $get_contract = $monthdb->find($this->post['id']);
        if ($get_contract) {
            $save_data = [];
            $save_data['audit_id'] = MEMBER_ID;
            $save_data['audit_time'] = date('Y-m-d H:i:s');
            $save_data['audit'] = 2;
            $save_data['reason'] = $this->post['remark'];
            $get_contract->save($save_data);
        }
        $this->layuiReturn('驳回成功', 1, '驳回成功', 1);
    }

    // 同步发票文件
    public function snyState()
    {
        set_time_limit(3600);
        ini_set('memory_limit', '256M');
        $files = request()->file();
        vendor("PHPExcel.PHPExcel");
        $phpExcel = new \PHPExcel ();
        // 上传文件
        $order_arr = [];
        foreach ($files as $file) {
            $info = $file->validate([
                'site' => 1024 * 10 * 1024 * 1024,
                'ext' => 'xls,xlsx'
            ])->move('./uploads/');
            if ($info) {
                $fileName = './uploads/' . $info->getSaveName();
                $objReader = \PHPExcel_IOFactory::createReader('Excel5');
                $objReader->setReadDataOnly(true);
                $objPHPExcel = $objReader->load($fileName, $encode = 'utf-8');
                $sheet = $objPHPExcel->getSheet();
                $get_row_num = $sheet->getHighestRow(); // 总行数
                $the_sheet = $objPHPExcel->getActiveSheet();
                $monthdb = new MonthContractdb();
                $all_data = [];
                for ($i = 2; $i <= $get_row_num; $i++) {
                    $save_data = [];
                    $save_data['month'] = (string)$this->getSheetValue($the_sheet, "A" . $i);
                    $save_data['supplier_id'] = $this->getSheetValue($the_sheet, "B" . $i);
                    $save_data['audit'] = (int)$this->getSheetValue($the_sheet, 'C' . $i);
                    $save_data['reason'] = $this->getSheetValue($the_sheet, 'D' . $i);
                    $save_data['audit_id'] = MEMBER_ID;
                    $save_data['audit_time'] = date('Y-m-d H:i:s');
                    $get_contract = $monthdb
                        ->where('month', $save_data['month'])
                        ->where('supplier_id', $save_data['supplier_id'])
                        ->find();
                    if ($get_contract) {
                        $save_data['id'] = $get_contract['id'];
                        array_push($all_data, $save_data);
                    }
                }
                if (count($all_data)) {
                    $monthdb->saveAll($all_data);
                }
                $this->layuiReturn('同步完成', 0, '同步完成', 1);
            } else {
                $this->layuiReturn($file->getError(), 0, $file->getError(), 0);
            }
        }
    }

    /**
     * Title("校验是否上传合同")
     */
    public function check(){
        $monthdb = new MonthContractdb();
        $get_contract = $monthdb
            ->where('supplier_id',$this->post['supplier_id'])
            ->where('month',date('Y-m',strtotime('-1 month')))
            ->find();
        if($get_contract){
            if($get_contract['audit'] != 1){
                $this->layuiReturn(1);
            }
        }
        $this->layuiReturn(0);
    }

}